﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuoiHoi.Business;
using CuoiHoi.Entities;
using System.Web.Services;
using System.IO;

namespace CuoiHoi.Admin
{
    public partial class ucQuanLyAlbumAnh : System.Web.UI.UserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        { 
                LoadCbbAlbum();
        }

        void LoadCbbAlbum()
        {
            cbbTenAlbum.DataSource = BAlbum.SelectAll();
            cbbTenAlbum.DataTextField = "TenAlbum";
            cbbTenAlbum.DataValueField = "idAlbum";
            cbbTenAlbum.DataBind();
            cbbTenAlbum.Items.Insert(0, "--Chọn Album  --");
        }

       


            void LoadData(int idAlbum)
            {

                DataTable dt = new DataTable();
                dt = BHinhAnh.SelectHinhAnhByAlbum(idAlbum);
                PagedDataSource pgitems = new PagedDataSource();
                System.Data.DataView dv = new System.Data.DataView(dt);
                pgitems.DataSource = dv;
                pgitems.AllowPaging = true;
                // 14 ảnh hiển thị ra ngoài
                pgitems.PageSize = 14;
                pgitems.CurrentPageIndex = PageNumber;
                if (pgitems.PageCount > 1)
                {
                    rptPages.Visible = true;
                    System.Collections.ArrayList pages = new System.Collections.ArrayList();
                    for (int i = 0; i < pgitems.PageCount; i++)
                        pages.Add((i + 1).ToString());
                    rptPages.DataSource = pages;
                    rptPages.DataBind();
                }
                else
                    rptPages.Visible = false;
                Repeater1.DataSource = pgitems;
                Repeater1.DataBind();
            }
            public int PageNumber
            {
                get
                {
                    if (ViewState["PageNumber"] != null)
                        return Convert.ToInt32(ViewState["PageNumber"]);
                    else
                        return 0;
                }
                set
                {
                    ViewState["PageNumber"] = value;
                }
            }
            protected void rptPages_ItemCommand1(object source, RepeaterCommandEventArgs e)
            {
                PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
                int idAlbum = int.Parse(cbbTenAlbum.SelectedValue.ToString());
                LoadData(idAlbum);
            }
     

            protected void cbbTenAlbum_TextChanged(object sender, EventArgs e)
            {
                try
                {
                    int idAlbum = int.Parse(cbbTenAlbum.SelectedValue.ToString());
                    LoadData(idAlbum);
                    Label1.Text = cbbTenAlbum.SelectedValue.ToString();
                }
                catch  {}              
            }

            protected void btnXoaAlbum_Click(object sender, EventArgs e)
            {            

                int IdAlbum = int.Parse(cbbTenAlbum.SelectedValue.ToString());        

                // xóa file 
                DataTable DsXoa = new DataTable();

                DsXoa = BHinhAnh.SelectHinhAnhByAlbum(IdAlbum);

                foreach (DataRow  dr in DsXoa.Rows)
                {
                    string tenfile = dr["DuongDanHinh"].ToString();
                    string DuongDan = System.Web.HttpContext.Current.Server.MapPath("~/images/") + tenfile;
                    if (File.Exists(DuongDan))
                    {
                        File.Delete(DuongDan);
                    }
                }

                // xóa trong bảng album
                BAlbum.Delete(IdAlbum);
                // xóa trong bảng hình ảnh
                BHinhAnh.Delete(IdAlbum);
                Response.Redirect("~/Admin/Default.aspx?idpage=qlha");
            }


    }
}